package com.example.test.model;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

public class ImageFactory {
    public static Bitmap scaleImg(int width,int height,Bitmap source )
    {
    	float sw=source.getWidth();
    	float sh=source.getHeight();
    	float ew,eh;
    	//原图片部分小于显示图片
    	if(sw<width||sh<height)
    	{
    		float wtimes=width/sw;
        	float htimes=height/sh;
        	if(wtimes>htimes)
        	{
        		ew=sw*(wtimes);
        		eh=sh*(wtimes);
        	}
        	else
        	{
        		ew=sw*(htimes);
        		eh=sh*(htimes);
        	}
    	}
    	//宽度高度均大于显示图片
    	else
    	{
    		float wtimes=sw/width;
        	float htimes=sh/height;
        	if(wtimes>htimes)
        	{
        		ew=sw/(htimes);
        		eh=sh/(htimes);
        	}
        	else
        	{
        		ew=sw/(wtimes);
        		eh=sh/(wtimes);
        	}
    	}
    	
    	Bitmap eimg = Bitmap.createScaledBitmap(source, (int)ew+1,(int) eh+1, true);
    	int xpos=(int)(ew-width)/2;
    	int ypos=(int)(eh-height)/2;
    	if(xpos<0)
    	{
    		System.out.println();
    	}
    	eimg=Bitmap.createBitmap(eimg, xpos, ypos, width, height);
    	return eimg;
    }
}
